using System;
using System.Collections.Generic;
using System.Data.SQLite;
using Model;

namespace DAL {
    public class OrderProductDAL {
        public List<OrderProductDTO> SelectAllByOrderId(int orderId) {
            List<OrderProductDTO> dtos = new List<OrderProductDTO>();
            SQLiteDataReader sqLiteDataReader = SqlHelper.GetDataReader(
                new SQLiteCommand("select p.ProId,p.ProName,p.ProPrice,op.ProCount,p.ProUnit,op.ProCount * p.ProPrice totalPrice,c.CatName,op.SubTime from ProductInfo p, CategoryInfo c, Order_Product op where p.CatId = c.CatId and op.ProId = p.ProId and op.OrderId = @orderId and op.DelFlag = 0") {
                    Parameters = {
                        new SQLiteParameter("@orderId", orderId)
                    }
                });
            while (sqLiteDataReader.Read()) {
                dtos.Add(new OrderProductDTO() {
                    ProId = int.Parse(sqLiteDataReader["ProId"].ToString()),
                    ProName = sqLiteDataReader["ProName"].ToString(),
                    ProPrice = Convert.ToDecimal(sqLiteDataReader["ProPrice"].ToString()),
                    ProCount = int.Parse(sqLiteDataReader["ProCount"].ToString()),
                    ProUnit = sqLiteDataReader["ProUnit"].ToString(),
                    totalPrice = Convert.ToDecimal(sqLiteDataReader["totalPrice"].ToString()),
                    CatName = sqLiteDataReader["CatName"].ToString(),
                    SubTime = Convert.ToDateTime(sqLiteDataReader["SubTime"].ToString())
                });
            }

            return dtos;
        }

        public int SelectByOPID(OrderProduct orderProduct) {
            object o = SqlHelper.GetSingleResult(
                new SQLiteCommand("select ProCount from Order_Product where OrderId = @orderId and ProId = @proId and DelFlag = 0") {
                    Parameters = {
                        new SQLiteParameter("@orderId", orderProduct.OrderId),
                        new SQLiteParameter("@proId", orderProduct.ProId)
                    }
                });
            if (o == null) {
                return 0;
            }
            return int.Parse(o.ToString());
        }

        public void Update(OrderProduct orderProduct) {
            SqlHelper.Update(new SQLiteCommand("update Order_Product set ProCount=@proCount where OrderId = @orderId and ProId = @ProId") {
                Parameters = {
                    new SQLiteParameter("@orderId", orderProduct.OrderId),
                    new SQLiteParameter("@proCount", orderProduct.ProCount),
                    new SQLiteParameter("@ProId", orderProduct.ProId)
                }
            });
        }

        public void Insert(OrderProduct orderProduct) {
            SqlHelper.Update(
                new SQLiteCommand(
                    "insert into Order_Product(OrderId,ProId,ProCount,SubTime) values(@orderId,@proId,@proCount,@subTime)") {
                    Parameters = {
                        new SQLiteParameter("@orderId", orderProduct.OrderId),
                        new SQLiteParameter("@proId", orderProduct.ProId),
                        new SQLiteParameter("@proCount", orderProduct.ProCount),
                        new SQLiteParameter("@subTime", orderProduct.SubTime)
                    }
            });
        }

        public void Delete(OrderProduct orderProduct) {
            SqlHelper.Update(new SQLiteCommand("update Order_Product set ProCount=@proCount,SubTime = @SubTime, DelFlag = @DelFlag where OrderId = @orderId and ProId = @ProId") {
                Parameters = {
                    new SQLiteParameter("@orderId", orderProduct.OrderId),
                    new SQLiteParameter("@proCount", orderProduct.ProCount),
                    new SQLiteParameter("@DelFlag", orderProduct.DelFlag),
                    new SQLiteParameter("@ProId", orderProduct.ProId),
                    new SQLiteParameter("@SubTime", orderProduct.SubTime)
                    
                }
            });
        }

        public void Checkout(int orderId) {
            SqlHelper.Update(new SQLiteCommand("update Order_Product set DelFlag = 1 where OrderId = @OrderId") {
                Parameters = { new SQLiteParameter("@OrderId", orderId) }
            });
        }
    }
}